package com.dykj.fireFighting.web.cron;

import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling// 2.开启定时任务
@Transactional
public class AreaSecheduling {
    @DubboReference
    private IEnterpriseService iEnterpriseService;
    @Autowired
    private RedisService redisService;
    @DubboReference
    private ISysDirectoryService iSysDirectoryService;

    /**
     * 每分钟运行一次，检测监控中心对应的下属单位
     * @throws Exception
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void check() throws Exception {

        Map<String,Object> map=new HashMap<>();
        map.put("enterprise_type",2);
        map.put("status",0);
        List<Enterprise> list =iEnterpriseService.listByMap(map);
        for (Enterprise e:list)
        {
            SysDirectory sysDirectory=iSysDirectoryService.queryByCode(e.getAreaCode()) ;
            if(sysDirectory.getLevel()==2)
            {
                redisService.set(e.getAreaCode(),e.getBindingCode());
            }else{
                String areaCode=null;
                //获取area对应的绑定码
                if(sysDirectory.getLevel()==0)
                {
                    areaCode=e.getAreaCode().substring(0,2);
                }
                if(sysDirectory.getLevel()==1)
                {
                    areaCode=e.getAreaCode().substring(0,4);
                }
                List<String> codeList=iEnterpriseService.queryBindCodeByUnitAreaCode(areaCode);
                String str = StringUtils.join(codeList.toArray(), ",");
                redisService.set(e.getAreaCode(),str);


            }


        }

    }

}
